package com.jic.veteran.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.jic.veteran.common.pay.SybConstants;
import com.jic.veteran.common.pay.SybPayV2Util;
import com.jic.veteran.common.pay.SybUtil;
import com.jic.veteran.mapper.BigScreenTimeInfoMapper;
import com.jic.veteran.mapper.PayInfoMapper;
import com.jic.veteran.model.BigScreenTimeInfo;
import com.jic.veteran.model.PayInfo;
import com.jic.veteran.service.BigScreenTimeService;
import com.jic.veteran.service.DonatePayService;
import com.jic.veteran.service.UmsAdminCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Slf4j
@Service
public class BigScreenTimeServiceImpl implements BigScreenTimeService {
    @Autowired
    private BigScreenTimeInfoMapper bigScreenTimeInfoMapper;
    @Autowired
    private UmsAdminCacheService umsAdminCacheService;
    @Override
    public int insert(BigScreenTimeInfo bigScreenTimeInfo) {
        int insert = bigScreenTimeInfoMapper.insert(bigScreenTimeInfo);
        return insert;
    }

    @Override
    public BigScreenTimeInfo get(Long id) {
        return bigScreenTimeInfoMapper.get(id);
    }

    @Override
    public List<BigScreenTimeInfo> getList() {
        return bigScreenTimeInfoMapper.getList();
    }

    @Override
    public int updateStatus(Long id, String status) {
        if("1".equals(status)) {
            List<BigScreenTimeInfo> bigScreenTimeInfoList = this.getList();
            for (BigScreenTimeInfo bigScreenTimeInfo:bigScreenTimeInfoList) {
                if(!bigScreenTimeInfo.getId().equals(id) && "1".equals(bigScreenTimeInfo.getStatus())) {
                    return -1;
                }
            }
        }
        BigScreenTimeInfo bigScreenTimeInfo = this.get(id);
        if(ObjectUtil.isNotEmpty(bigScreenTimeInfo)) {
            int updateStatus = bigScreenTimeInfoMapper.updateStatus(id, status);
            if(updateStatus >0) {
                if (StrUtil.equals(status,"1")) {
                    umsAdminCacheService.setBigScreenTime(bigScreenTimeInfo.getBigScreenTime());
                }
                if (StrUtil.equals(status,"0")) {
                    umsAdminCacheService.removeBigScreenTime();
                }
                return updateStatus;
            }
        }
        return 0;
    }

    @Override
    public int update(Long id, String bigScreenTime) {
        BigScreenTimeInfo bigScreenTimeInfo = this.get(id);
        if(ObjectUtil.isNotEmpty(bigScreenTimeInfo)) {
            int update = bigScreenTimeInfoMapper.update(id,bigScreenTime);
            return update;
        }
        return 0;
    }

    @Override
    public int delete(Long id) {
        BigScreenTimeInfo bigScreenTimeInfo = this.get(id);
        if(ObjectUtil.isNotEmpty(bigScreenTimeInfo)) {
            if("1".equals(bigScreenTimeInfo.getStatus())) {
                return -1;
            }
            int delete = bigScreenTimeInfoMapper.delete(id);
            if(delete > 0) {
                umsAdminCacheService.removeBigScreenTime();
            }
            return delete;
        }
        return 0;
    }
}
